package com.lagou.mr.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.FileUtil;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Properties;

public class RankDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {

        //读取配置，省去纠结输入输出路径传参配置
        Properties cfg = null;
        try {
            cfg = CfgUtil.getProp();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String strIn = cfg.getProperty(Constant.INPUT_DIR);
        System.out.println("==> input = " + strIn);
        String strOut = cfg.getProperty(Constant.OUTPUT_DIR);
        System.out.println("==> output = " + strOut);
        File outFolder = new File(strOut);
        String[] list = FileUtil.list(outFolder);
        if(list.length > 0)
        {
            FileUtil.fullyDelete(outFolder);
        }

        final Configuration conf = new Configuration();
        final Job job = Job.getInstance(conf, "RankDriver");
        job.setJarByClass(RankDriver.class);
        job.setMapperClass(RankMapper.class);
        job.setReducerClass(RankReducer.class);
        job.setMapOutputKeyClass(RankBean.class);
        job.setMapOutputValueClass(NullWritable.class);
        job.setOutputKeyClass(RankBean.class);
        job.setOutputValueClass(NullWritable.class);

        FileInputFormat.setInputPaths(job, new Path(strIn));
        FileOutputFormat.setOutputPath(job, new Path(strOut));
        final boolean flag = job.waitForCompletion(true);
        System.exit(flag ? 0 : 1);
    }


}
